package dao;
import java.util.List;

import org.hibernate.Transaction;
import org.hibernate.classic.Session;

import util.HibernateUtil;
import entity.Cliente;
import entity.Compra;

public class CompraDaoImplementation implements CompraDao {

	public void save(Compra compra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.save(compra);
		transaction.commit();
		session.close();
	}

	public void update(Compra compra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.update(compra);
		transaction.commit();
		session.close();
	}

	public void delete(Compra compra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		session.delete(compra);
		transaction.commit();
		session.close();
	}

	public void consult(Compra compra) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();
		compra = (Compra) session.load(Compra.class, new String("Compra"));
	}

	public Compra getCompra(long id) 
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		return (Compra) session.load(Compra.class, id);
	}

	@Override
	public List<Compra> list() {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = session.beginTransaction();
        String sql = "SELECT new Compra( c.idCompra, c.nota, c.serie, c.dataEmissao, c.dataChegada, c.fornecedor, c.totalNota) FROM Compra c";
        List<Compra> lista = session.createQuery(sql).list();
        transaction.commit();
        session.close();
        return lista;
	}
	
	

}
